// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';

export default class extends think.Controller {
  /**
   * index action
   * @return {Promise} []
   */
  // 获取分店产品数据
  async getGoodsListAction() {
    const filter = this.get();
    const where = {};
    where['sg.isdisable'] = 0;
    where['sg.shopid'] = filter.shopid;
    if (filter.search) {
      where['s.name'] = ['like', '%' + filter.search + '%'];
    }
    const list = await this.model('or_shop_goods')
      .alias('sg')
      .field('sg.*, s.name, s.remarks, s.thumbnail')
      .join('think_or_goods s on sg.goodsid = s.id')
      .where(where)
      .select();
    return this.json(list);
  }

  /*
   * @desc 获取单个产品的信息
   * @return {object} 产品信息
   */
  async getGoodsInfoAction() {
    const id = this.get().id;
    const info = await this.model('or_shop_goods')
      .alias('sg')
      .field('sg.*, s.name, s.remarks, s.thumbnail, s.banner, s.desc')
      .join('think_or_goods s on sg.goodsid = s.id')
      .where({'sg.id': id})
      .find();
    return this.json(info);
  }
}
